import { Toast } from 'vant'
import axios, { AxiosRequestConfig, Method } from "axios";
import { createApp } from 'vue'
import App from '@/App.vue'
import i18n from "../../plugin/lang/i18n"
import { userMode } from '@/store/user'
import { I18n } from 'vue-i18n';


//报错提示
function errorMsg(msg: any) {
    Toast.fail(msg)
}

export default {
    install: (Vue: { use: (arg0: I18n<any, {}, {}, string, false>) => void; }) => {
        Vue.use(i18n)

        const app = createApp(App)
        app.config.globalProperties.$post = axios
        // 全局默认请求
        axios.defaults.baseURL = process.env.VUE_APP_REQUEST_URL
        axios.defaults.timeout = 1000 * 60



        // 覆盖header 
        interface requestImpl extends AxiosRequestConfig {
            headers?: any
        }

        let token: string | null
        if (process.env.VUE_APP_TOKEN_LOCAL === 'true') {
            token = localStorage.getItem('token')
        } else {
            token = userMode().getToken()
        }

        axios.interceptors.request.use(
            (config: requestImpl) => {
                if (token) {
                    config.headers['Authorization'] = `Bearer ${token}`
                }

                return config
            },
            (error: any) => {
                console.log(i18n);


            }
        )
    }

}



